package com.macro.mall.portal.controller;

import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsMemberBrandAttention;
import com.macro.mall.portal.service.MemberAttentionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员关注品牌管理Controller
 *
 * @author macro
 * @date 2018/8/2
 */
@Controller
@Api(tags = "MemberAttentionController")
@Tag(name = "MemberAttentionController", description = "会员关注品牌管理")
@RequestMapping("/member/attention")
@AllArgsConstructor
public class MemberAttentionController {
    private final MemberAttentionService memberAttentionService;

    @AccessLimit
    @ApiOperation("添加品牌关注")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> add(@RequestBody UmsMemberBrandAttention memberBrandAttention) {
        int count = memberAttentionService.add(memberBrandAttention);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("取消品牌关注")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> delete(Long brandId) {
        int count = memberAttentionService.delete(brandId);
        return count > 0 ? CommonResult.success(count) : CommonResult.failed();
    }

    @AccessLimit
    @ApiOperation("显示当前用户品牌关注列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<UmsMemberBrandAttention>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<UmsMemberBrandAttention> page = memberAttentionService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @AccessLimit
    @ApiOperation("显示品牌关注详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMemberBrandAttention> detail(@RequestParam Long brandId) {
        return CommonResult.success(memberAttentionService.detail(brandId));
    }

    @AccessLimit
    @ApiOperation("清空当前用户品牌关注列表")
    @RequestMapping(value = "/clear", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Object> clear() {
        memberAttentionService.clear();
        return CommonResult.success(null);
    }
}
